# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
  scipen = 999,
  digits = 16,
  max.print = .Machine$integer.max,
  show.error.locations = TRUE,
  warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------

library(data.table)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Just in case something masks 'source()', use extra deliberate base::source().
base::source("~/code/0-utility-functions/table-and-text-utils.R", local = TRUE)

# READ IN OUR DATA -------------------------------------------------------------

summary_dt <-
    readRDS("~/summary-output/covariates_treated_vs_control_dt.rds")

# MAKE TABLE -------------------------------------------------------------------

filedir <- "~/paper/tables/table-A.2.tex"
file.create(filedir)

# Start Table A.2
start_latex_table(column_structure = "lccc", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "toprule", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

heading_text_input1 <-
    " &  & \\multicolumn{2}{c}{Population} "
write_latex_table_row(heading_text_input1, file_dir = filedir, append = TRUE)

add_latex_cmidrule(
    point_start_col = "l",
    point_end_col = "r",
    start_col = 3,
    end_col = 4,
    file_dir = filedir,
    append = TRUE
)

heading_text_input2 <-
     " \\multirow{2}{*}{Covariate} & \\multirow{2}{*}{Statistic} & Treatment Group (Current Winners) & Control Group (Not-Yet Winners) "
write_latex_table_row(heading_text_input2, file_dir = filedir, append = TRUE)

heading_text_input3 <- "   &  & (1) & (2) "
write_latex_table_row(heading_text_input3, file_dir = filedir, append = TRUE)

heading_text_input1 <- NULL
heading_text_input2 <- NULL
heading_text_input3 <- NULL
rm(heading_text_input1, heading_text_input2, heading_text_input3)

add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

# Add body of Table A.2
wage_earnings_input <-
  sprintf(
    "Wage Earnings & Mean & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = summary_dt[treated == 1]$mean_wages,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[treated == 0]$mean_wages,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(wage_earnings_input, file_dir = filedir, append = TRUE)
wage_earnings_input <- NULL
rm(wage_earnings_input)

employment_input <-
  sprintf(
    "Employment & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[treated == 1]$mean_employment,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[treated == 0]$mean_employment,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(employment_input, file_dir = filedir, append = TRUE)
employment_input <- NULL
rm(employment_input)

age_input <-
  sprintf(
    "Age & Mean & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[treated == 1]$mean_age,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[treated == 0]$mean_age,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(age_input, file_dir = filedir, append = TRUE)
age_input <- NULL
rm(age_input)

gender_input <-
  sprintf(
    "Female & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[treated == 1]$mean_female,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[treated == 0]$mean_female,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(gender_input, file_dir = filedir, append = TRUE)
gender_input <- NULL
rm(gender_input)

marital_status_input <-
  sprintf(
    "Married & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[treated == 1]$mean_married,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[treated == 0]$mean_married,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(marital_status_input, file_dir = filedir, append = TRUE)
marital_status_input <- NULL
rm(marital_status_input)

homeowner_input <-
  sprintf(
    "Homeowner & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[treated == 1]$mean_home,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[treated == 0]$mean_home,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(homeowner_input, file_dir = filedir, append = TRUE)
homeowner_input <- NULL
rm(homeowner_input)

size_of_win_input <-
  sprintf(
    "Size of the Lottery Win & Mean & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = summary_dt[treated == 1]$mean_lottery_winning,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[treated == 0]$mean_lottery_winning,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(size_of_win_input, file_dir = filedir, append = TRUE)
size_of_win_input <- NULL
rm(size_of_win_input)

# End Table A.2
add_latex_table_rule(
  rule_type = "bottomrule",
  file_dir = filedir,
  append = TRUE
)
end_latex_table(file_dir = filedir, append = TRUE)
filedir <- NULL
rm(filedir)

# Housekeeping
summary_dt <- NULL
rm(summary_dt)